<?php
namespace app\api\controller;

use app\common\model as M;

/**
 *
 */
class Distrbute extends \app\api\ApiAuth
{
    public $model = '\app\common\model\DistributionSeparate';

    /**
     * 获取分享海报
     * @Author   FW
     * @DateTime 2021-09-23
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function get_share_info ()
    {
        $rel_type  = $this->request->post('rel_type' , '');
        $rel_id    = $this->request->post('rel_id' , 0);
        $share_url = $this->request->post('share_url' , '');

        if ( !$rel_type || !$rel_id || !$share_url ) return $this->returnFail('网络错误请稍候再试[101]！');

        $separate_cfg = $this->model::where(['relation_type' => $this->model::$reltion_type_mapping[$rel_type] , 'relation_id' => $rel_id])->find();

        if ( empty($separate_cfg) ) return $this->returnFail('网络错误请稍候再试[102]！');

        ##if ( !$separate_cfg->share_poster ) return $this->returnFail('网络错误请稍候再试[103]！');
        if ( !$separate_cfg->share_poster ) $separate_cfg->share_poster = dirname(__DIR__) . '/../../public/wap/first/dunixue/images/share-poster.png';

        $result="/public/uploads/qrcode/". $rel_type . "_" . $this->uid . "_" . $rel_id.".png";

        $poster_bg_info = getimagesize($separate_cfg->share_poster);
        $postion_x = $postion_y = 0;
        $uqrcode_w = 160;
        $uqrcode_h = 160;
        $postion_x = $poster_bg_info[0] - $uqrcode_h - 20;
        $postion_y = $poster_bg_info[1] - $uqrcode_h - 20;

        $user=$this->userInfo;

        $bac=$separate_cfg->share_poster;
        $QR =  dirname(__DIR__)."/../../public/uploads/qrcode/". $this->uid .".png";
        $last = dirname(__DIR__)."/../.." . $result;

        $errorCorrectionLevel = 'Q'; //防错等级
        $matrixPointSize = 8; //二维码大小

        vendor('phpqrcode.phpqrcode');
        \QRcode::png($share_url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);

        $bac = imagecreatefromstring(file_get_contents($bac));
        $qr = imagecreatefromstring(file_get_contents($QR));
        $qr_width = imagesx($qr);
        $qr_height = imagesy($qr);
        imagecopyresampled($bac, $qr, $postion_x, $postion_y, 0, 0, $uqrcode_w, $uqrcode_h, $qr_width, $qr_height);

        //输出到本地文件夹
        imagepng($bac,$last);
        $result="/public/uploads/qrcode/". $rel_type . "_" . $this->uid . "_" . $rel_id.".png";

        return $this->returnSuccess(['distributionShareImage' => $result]);
    }
}//Class End